(function () {
    angular.module('app')
        .component('xxDrugInfo',{
            templateUrl:'/app/user/drug-info/drug-info.html',
            controller:ctrl
        });

    function ctrl($scope,$state,$mdDialog,$stateParams,DrugInfoFactory,userService,urlCnst) {
        $scope.drug = [];
        $scope.username = userService.user['username'];
        $scope.userId = userService.user['userId'];

        var drugId = $stateParams.drugId;
        function getDrugInfoById(drugId) {
            function cb(err, res) {
                if (!err) $scope.drug = res['res'][0];
                // console.log(res['res']);
            }
            DrugInfoFactory.getDrugInfoById(drugId,cb);
        }
        getDrugInfoById(drugId);

        $scope.commodity = {
            // 'drugId':$scope.drug['drugId'],
            // 'userId':$scope.userId,
            // 'drugName':$scope.drug['drugName'],  // unneeded
            // 'img':$scope.drug['img'],   // unneeded
            // 'price':$scope.drug['price'],
            'drugCount':1
        };

        function showAlert() {
            $mdDialog.show(
                $mdDialog.alert()
                //.parent(angular.element(document.querySelector('#popupContainer')))
                    .clickOutsideToClose(true)
                    .title('您还没有登录，请先登录')
                    .textContent(' ')
                    .ariaLabel('Alert Dialog Demo')
                    .ok('好的')
                //.targetEvent(ev)
            );
        }

        $scope.addToCart = function (body) {
            if ( !$scope.username ) {
                showAlert();
                //urlCnst.notice('请先登录');
                return;
            }
            // var username = $scope.username;
            // body['userId'] = $scope.userId;
            // body['number'] = $scope.commodity['number'];

            function cb(err, res) {
                if (!err) urlCnst.notice("添加成功");
                console.log(res);
                console.log('userId',$scope.userId);
                console.log('drugId',drugId);
            }
            DrugInfoFactory.postDrugInCart($scope.userId, drugId, body, cb);
        }
    }
})();